// $Id: buf.h 2 2011-10-17 17:49:43Z cedric.shih@gmail.com $
/*
 * Copyright 2011 Cedric Shih (cedric dot shih at gmail dot com)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#ifndef BUFFER_H_
#define BUFFER_H_

#include <sys/queue.h>

#define TAILQ_FOREACH_SAFE(var, head, field, next_var)                  \
        for ((var) = ((head)->tqh_first);                               \
                (var) && ((next_var) = ((var)->field.tqe_next), 1);     \
                (var) = (next_var))

#include "list.h"

#include "zcbuffer.h"

enum zcbuffer_type {
	ZCBUFFER_UNKNOWN,
	ZCBUFFER_DIRECT,
	ZCBUFFER_ROTATED,
	ZCBUFFER_CLONED,
	ZCBUFFER_MAPPED,
};

struct zcbuffer {
	enum zcbuffer_type type;
	union {
		struct zcheap *heap;
		struct zcbuffer *buffer;
	} source;
	unsigned char *data;
	size_t offset; // offset to source data
	size_t capacity;
	size_t limit;
	size_t position;
	ssize_t mark;
	zcbuffer_cb cb;
	void *cbarg;
	struct zcbuffer_list clones;
	struct zcbuffer_list caches;
	struct zcbuffer_item item;
};

void zcbuffer_init(struct zcbuffer *buffer);

#endif /* BUFFER_H_ */
